{% extends base %}

{% block head %}
<script src="{{_server_home}}/_static/lib/codemirror/codemirror.min.js"></script>
<link rel="stylesheet" type="text/css" href="{{_server_home}}/_static/lib/codemirror/codemirror.min.css">
<link rel="stylesheet" type="text/css" href="{{_server_home}}/_static/lib/codemirror/theme/monokai.min.css">
<script type="text/javascript" src="{{_server_home}}/_static/lib/codemirror/mode/sql.min.js"></script>
<style>
.CodeMirror {
    width: 100%;
    font-size:16px;
    height: 80px;
    border: 1px solid #ccc;
}
</style>
{% end %}

{% block body %}
{% init error = "" %}
{% init cost_time = None %}
{% init path = "" %}
{% init path_list = [] %}
{% init sql = "" %}
{% init keys = [] %}
{% init embed = False %}

<div class="card">
    <div class="card-title">
        <span>{{T("SQLite管理器")}}</span>
        <div class="float-right">
            {% include common/button/back_button.html %}
        </div>
    </div>
</div>

<div class="card">
    {% include mod_fs_path.html %}
    <form>
        <input type="hidden" name="path" value="{{path}}">
        <input type="hidden" name="type" value="{{type}}">
        <input type="hidden" name="embed" value="{{embed}}">
        <textarea id="editor" name="sql" style="width:100%;">{{sql}}</textarea>
        <button class="btn">运行</button>
        <a class="btn btn-default" href="?path={{path}}&action=show_tables&embed={{embed}}">展示表</a>
        <a class="btn btn-default" href="?path={{path}}&action=count_tables&embed={{embed}}">记录数统计</a>
        <a class="btn btn-default" onclick="xnoteImportBackup()">导入备份</a>
    </form>
</div>

<div class="card">

    {% if cost_time is not None %}
    <div class="col-md-12">
    耗时: {{cost_time}} ms
    </div>
    {% end %}

    {% if error != "" %}
        <pre class="col-md-12 error">
        {{error}}
        </pre>
    {% end %}

    <div class="row">        
        {% include common/table/table.html %}
    </div>

</div>

<script type="text/javascript">
$(function () {
    var editor = $("#editor")[0];
    var codeMirror = CodeMirror.fromTextArea(editor, {
        lineNumbers:true,
        mode: "text/x-sql"
    });

    xnote.table.adjustWidth(".table");
});


window.xnoteImportBackup = function () {
    xnote.confirm("导入数据会覆盖现有数据，确认执行?", function (resp) {
        $.get("{{_server_home}}/system/backup?p=import_db&path={{quote(path)}}")
        xnote.alert("执行中... <a target=\"_blank\" href=\"{{_server_home}}/system/log?log_type=mem&log_name=import_db\">查看执行日志</a>")
    });
};

</script>

{% end %}